package com.example.agendamentoconsultas;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;


public class Hora {

	private int traduzDataHora(Date data){
		int retorno;
		
		Calendar calendario = Calendar.getInstance();
		calendario.setTime(data);
	    
		if (calendario.get(Calendar.DAY_OF_WEEK)>=2 && calendario.get(Calendar.DAY_OF_WEEK)<=6){
			retorno = calendario.get(Calendar.DAY_OF_WEEK) * 100;
			if (calendario.get(Calendar.HOUR_OF_DAY)>=8 && calendario.get(Calendar.HOUR_OF_DAY)<=12){
				retorno = retorno + 1;
			}else if(calendario.get(Calendar.HOUR_OF_DAY)>=14 && calendario.get(Calendar.HOUR_OF_DAY)<=18){
				retorno = retorno + 10;
			}else if(calendario.get(Calendar.HOUR_OF_DAY)<8){
				retorno = retorno + 1;
			}else if(calendario.get(Calendar.HOUR_OF_DAY)>=12 && calendario.get(Calendar.HOUR_OF_DAY)<14){
				retorno = retorno + 10;
			}else if(calendario.get(Calendar.HOUR_OF_DAY)>=18){
				retorno = 888;
			}else{
				retorno = 999;
			}
		}else{
			retorno = 201;
		}
		return retorno;
	}
	
	public ArrayList<DataHora> geraHorarios(Date data, Horario_Medico hm){
		int codHora = traduzDataHora(data);
		
		ArrayList<DataHora> horarios = new ArrayList<DataHora>();

		Calendar calendario = Calendar.getInstance();
		calendario.setTime(data);
		
		if (codHora==888){
			calendario.add(Calendar.DAY_OF_MONTH, 1);
			data = new GregorianCalendar(calendario.get(Calendar.YEAR),calendario.get(Calendar.MONTH),calendario.get(Calendar.DAY_OF_MONTH)).getTime();
			codHora = traduzDataHora(data);
		}
		
		if (codHora == 201){
			if (hm.isSeg_m()){
				horarios.addAll(montaManha(calendario));
			}
			data = new GregorianCalendar(calendario.get(Calendar.YEAR),calendario.get(Calendar.MONTH),calendario.get(Calendar.DAY_OF_MONTH),14,0).getTime();
			codHora = traduzDataHora(data);
		}
		if (codHora == 210){
			if (hm.isSeg_t()){
				montaTarde(calendario);
			}
			calendario.add(Calendar.DAY_OF_MONTH, 1);
			data = new GregorianCalendar(calendario.get(Calendar.YEAR),calendario.get(Calendar.MONTH),calendario.get(Calendar.DAY_OF_MONTH)).getTime();
			codHora = traduzDataHora(data);
		}
		if (codHora == 301){
			if (hm.isTer_m()){
				horarios.addAll(montaManha(calendario));
			}
			data = new GregorianCalendar(calendario.get(Calendar.YEAR),calendario.get(Calendar.MONTH),calendario.get(Calendar.DAY_OF_MONTH),14,0).getTime();
			codHora = traduzDataHora(data);
		}
		if (codHora == 310){
			if (hm.isTer_t()){
				horarios.addAll(montaTarde(calendario));
			}
			calendario.add(Calendar.DAY_OF_MONTH, 1);
			data = new GregorianCalendar(calendario.get(Calendar.YEAR),calendario.get(Calendar.MONTH),calendario.get(Calendar.DAY_OF_MONTH)).getTime();
			codHora = traduzDataHora(data);
		}
		if (codHora == 401){
			if (hm.isQua_m()){
				horarios.addAll(montaManha(calendario));
			}
			data = new GregorianCalendar(calendario.get(Calendar.YEAR),calendario.get(Calendar.MONTH),calendario.get(Calendar.DAY_OF_MONTH),14,0).getTime();
			codHora = traduzDataHora(data);
		}
		if (codHora == 410){
			if (hm.isQua_t()){
				horarios.addAll(montaTarde(calendario));
			}
			calendario.add(Calendar.DAY_OF_MONTH, 1);
			data = new GregorianCalendar(calendario.get(Calendar.YEAR),calendario.get(Calendar.MONTH),calendario.get(Calendar.DAY_OF_MONTH)).getTime();
			codHora = traduzDataHora(data);
		}
		if (codHora == 501){
			if (hm.isQui_m()){
				horarios.addAll(montaManha(calendario));
			}
			data = new GregorianCalendar(calendario.get(Calendar.YEAR),calendario.get(Calendar.MONTH),calendario.get(Calendar.DAY_OF_MONTH),14,0).getTime();
			codHora = traduzDataHora(data);
		}
		if (codHora == 510){
			if (hm.isQui_t()){
				horarios.addAll(montaTarde(calendario));
			}
			calendario.add(Calendar.DAY_OF_MONTH, 1);
			data = new GregorianCalendar(calendario.get(Calendar.YEAR),calendario.get(Calendar.MONTH),calendario.get(Calendar.DAY_OF_MONTH)).getTime();
			codHora = traduzDataHora(data);
		}
		if (codHora == 601){
			if (hm.isSex_m()){
				horarios.addAll(montaManha(calendario));
			}
			data = new GregorianCalendar(calendario.get(Calendar.YEAR),calendario.get(Calendar.MONTH),calendario.get(Calendar.DAY_OF_MONTH),14,0).getTime();
			codHora = traduzDataHora(data);
		}
		if (codHora == 610){
			if (hm.isSex_t()){
				horarios.addAll(montaTarde(calendario));
			}
			calendario.add(Calendar.DAY_OF_MONTH, 1);
			data = new GregorianCalendar(calendario.get(Calendar.YEAR),calendario.get(Calendar.MONTH),calendario.get(Calendar.DAY_OF_MONTH)).getTime();
			codHora = traduzDataHora(data);
		}
		
		return horarios;
	}
	
	private ArrayList<DataHora> montaManha(Calendar calendario){
		ArrayList<DataHora> retorno = new ArrayList<DataHora>();
		
		retorno.add(new DataHora(calendario.get(Calendar.DAY_OF_MONTH)+"/"+(calendario.get(Calendar.MONTH)+1)+"/"+calendario.get(Calendar.YEAR),"8:00"));
		retorno.add(new DataHora(calendario.get(Calendar.DAY_OF_MONTH)+"/"+(calendario.get(Calendar.MONTH)+1)+"/"+calendario.get(Calendar.YEAR),"8:30"));
		retorno.add(new DataHora(calendario.get(Calendar.DAY_OF_MONTH)+"/"+(calendario.get(Calendar.MONTH)+1)+"/"+calendario.get(Calendar.YEAR),"9:00"));
		retorno.add(new DataHora(calendario.get(Calendar.DAY_OF_MONTH)+"/"+(calendario.get(Calendar.MONTH)+1)+"/"+calendario.get(Calendar.YEAR),"9:30"));
		retorno.add(new DataHora(calendario.get(Calendar.DAY_OF_MONTH)+"/"+(calendario.get(Calendar.MONTH)+1)+"/"+calendario.get(Calendar.YEAR),"10:00"));
		retorno.add(new DataHora(calendario.get(Calendar.DAY_OF_MONTH)+"/"+(calendario.get(Calendar.MONTH)+1)+"/"+calendario.get(Calendar.YEAR),"10:30"));
		retorno.add(new DataHora(calendario.get(Calendar.DAY_OF_MONTH)+"/"+(calendario.get(Calendar.MONTH)+1)+"/"+calendario.get(Calendar.YEAR),"11:00"));
		retorno.add(new DataHora(calendario.get(Calendar.DAY_OF_MONTH)+"/"+(calendario.get(Calendar.MONTH)+1)+"/"+calendario.get(Calendar.YEAR),"11:30"));
		
		return retorno;
	}
	
	private ArrayList<DataHora> montaTarde(Calendar calendario){
		ArrayList<DataHora> retorno = new ArrayList<DataHora>();
		
		retorno.add(new DataHora(calendario.get(Calendar.DAY_OF_MONTH)+"/"+(calendario.get(Calendar.MONTH)+1)+"/"+calendario.get(Calendar.YEAR),"14:00"));
		retorno.add(new DataHora(calendario.get(Calendar.DAY_OF_MONTH)+"/"+(calendario.get(Calendar.MONTH)+1)+"/"+calendario.get(Calendar.YEAR),"14:30"));
		retorno.add(new DataHora(calendario.get(Calendar.DAY_OF_MONTH)+"/"+(calendario.get(Calendar.MONTH)+1)+"/"+calendario.get(Calendar.YEAR),"15:00"));
		retorno.add(new DataHora(calendario.get(Calendar.DAY_OF_MONTH)+"/"+(calendario.get(Calendar.MONTH)+1)+"/"+calendario.get(Calendar.YEAR),"15:30"));
		retorno.add(new DataHora(calendario.get(Calendar.DAY_OF_MONTH)+"/"+(calendario.get(Calendar.MONTH)+1)+"/"+calendario.get(Calendar.YEAR),"16:00"));
		retorno.add(new DataHora(calendario.get(Calendar.DAY_OF_MONTH)+"/"+(calendario.get(Calendar.MONTH)+1)+"/"+calendario.get(Calendar.YEAR),"16:30"));
		retorno.add(new DataHora(calendario.get(Calendar.DAY_OF_MONTH)+"/"+(calendario.get(Calendar.MONTH)+1)+"/"+calendario.get(Calendar.YEAR),"17:00"));
		retorno.add(new DataHora(calendario.get(Calendar.DAY_OF_MONTH)+"/"+(calendario.get(Calendar.MONTH)+1)+"/"+calendario.get(Calendar.YEAR),"17:30"));
		
		return retorno;
	}
}
